package com.oa.pm.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.dto.HandoverAddDTO;
import com.oa.pm.dto.HandoverAuditDTO;
import com.oa.pm.entity.Handover;
import com.oa.pm.entity.SpecificHandoverItems;
import com.oa.pm.mapper.HandoverMapper;
import com.oa.pm.service.IHandoverService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.service.ISpecificHandoverItemsService;
import com.oa.pm.vo.HandoverDetailsVO;
import com.oa.pm.vo.HandoverListVO;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.dto.ApprovalDTO;
import com.oa.system.dto.HandoverListDTO;
import com.oa.system.service.IApplyApprovalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 离职交接表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class HandoverServiceImpl extends ServiceImpl<HandoverMapper, Handover> implements IHandoverService {

    private final HandoverMapper handoverMapper;

    @Autowired
    private ISpecificHandoverItemsService specificHandoverItemsService;

    @Autowired
    public HandoverServiceImpl(HandoverMapper handoverMapper) {
        this.handoverMapper = handoverMapper;
    }

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Override
    @CommonService
    public PageResult<Handover> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<Handover> list = handoverMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Handover> page = new Page<>(pageNum, pageSize);
            //查询
            List<Handover> list = handoverMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public HandoverDetailsVO findHandoverById(String id){
        return handoverMapper.findHandoverDetails(id);
    }

    @Override
    @CommonService
    public PageResult<HandoverListVO> findHandoverList(HandoverListDTO handoverListDTO) {
        Page page = handoverListDTO.getPage();
        return new PageResult<>(handoverMapper.findHandoverList(page, handoverListDTO), page.getPages());
    }

    @Override
    @CommonService
    public Resp add(String userId, HandoverAddDTO handoverAddDTO) throws BusinessException, DataIsEmptyException {
        Handover handover = new Handover();
        BeanUtils.copyProperties(handoverAddDTO, handover);

        if(insert(handover)){
            List<SpecificHandoverItems> itemsList = new ArrayList<>();
            handoverAddDTO.getSpecificHandoverItemsAddDTOS().forEach(d -> {
                SpecificHandoverItems items = new SpecificHandoverItems();
                BeanUtils.copyProperties(d, items);
                items.setHandoverId(handover.getId());
                itemsList.add(items);
            });
            if(specificHandoverItemsService.insertBatch(itemsList)){
                ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
                applyApprovalAddDTO.setApplyId(handover.getId());
                applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.RESIGNATION_HANDOVER.getId());
                applyApprovalAddDTO.setApplyApprovalName(handoverAddDTO.getName() + ProcessDefineEnum.RESIGNATION_HANDOVER.getText());
                applyApprovalAddDTO.setApprovalUserId(userId);
                return applyApprovalService.addApproval(applyApprovalAddDTO);
            }
        }
        return new Resp().failure();
    }

    @Transactional
    @Override
    @CommonService
    public Resp audit(HandoverAuditDTO handoverAuditDTO) throws DataIsEmptyException {
        ApprovalDTO approvalDTO = new ApprovalDTO();
        approvalDTO.setId(Long.valueOf(handoverAuditDTO.getId()));
        approvalDTO.setUserId("1");
        approvalDTO.setStatus(handoverAuditDTO.getAuditStatus());

        return applyApprovalService.audit(approvalDTO, x -> {
            List<SpecificHandoverItems> itemsList = new ArrayList<>();
            handoverAuditDTO.getSpecificHandoverItemsAddDTOS().forEach(d -> {
                SpecificHandoverItems items = new SpecificHandoverItems();
                BeanUtils.copyProperties(d, items);
                items.setHandoverId(handoverAuditDTO.getApplyId());
                itemsList.add(items);
            });
            specificHandoverItemsService.insertBatch(itemsList);
        });
    }
}
